/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package window;

import dao.DAOFactory;
import data.Dinheiro;
import data.Empregado;
import data.Emprestimo;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import services.empregado.EmpregadoServices;
import services.emprestimo.EmprestimoServices;

/**
 *
 * @author
 * PedroLZ
 */
public class DlgSolicitarEmprestimo extends javax.swing.JDialog
{
	public DlgSolicitarEmprestimo(java.awt.Frame parent, boolean modal, String cpfDoSolicitante) {
		super(parent, modal);
		initComponents();
		initForm( cpfDoSolicitante );
		SupportWindow.centralizaJanela( this );
	}

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTituloValorPrestacao = new javax.swing.JLabel();
        lblTituloCpf = new javax.swing.JLabel();
        lblTituloValor = new javax.swing.JLabel();
        lblConteudoNome = new javax.swing.JLabel();
        lblTituloNumParcelas = new javax.swing.JLabel();
        lblConteudoCpf = new javax.swing.JLabel();
        txtValorSolicitado = new javax.swing.JTextField();
        txtNumParcelas = new javax.swing.JTextField();
        separador = new javax.swing.JSeparator();
        btnSolicitar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblTituloEmpregado = new javax.swing.JLabel();
        lblTituloValorTotal = new javax.swing.JLabel();
        lblConteudoValorPrestacao = new javax.swing.JLabel();
        lblConteudoValorTotal = new javax.swing.JLabel();
        lblObrigatorio = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Solicitar Empréstimo");

        lblTituloValorPrestacao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTituloValorPrestacao.setText("Valor da Prestação: ");

        lblTituloCpf.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTituloCpf.setText("CPF: ");

        lblTituloValor.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTituloValor.setText("Valor*: ");

        lblConteudoNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblConteudoNome.setText("(nome do empregado)");

        lblTituloNumParcelas.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTituloNumParcelas.setText("N.º Parcelas*: ");

        lblConteudoCpf.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblConteudoCpf.setText("(cpf do empregado)");

        btnSolicitar.setText("Solicitar");
        btnSolicitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSolicitarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblTituloEmpregado.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTituloEmpregado.setText("Empregado: ");

        lblTituloValorTotal.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblTituloValorTotal.setText("Valor Total: ");

        lblConteudoValorPrestacao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblConteudoValorPrestacao.setText("(valor prestação)");

        lblConteudoValorTotal.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblConteudoValorTotal.setText("(valor total)");

        lblObrigatorio.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblObrigatorio.setText("*Obrigatório.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTituloCpf)
                            .addComponent(lblTituloValor)
                            .addComponent(lblTituloNumParcelas)
                            .addComponent(lblTituloEmpregado))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblConteudoCpf)
                            .addComponent(lblConteudoNome)
                            .addComponent(txtValorSolicitado, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNumParcelas, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(separador, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSolicitar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnCancelar)))
                        .addGap(26, 26, 26))))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTituloValorPrestacao)
                            .addComponent(lblTituloValorTotal))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblConteudoValorTotal)
                            .addComponent(lblConteudoValorPrestacao)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(lblObrigatorio)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblConteudoNome)
                    .addComponent(lblTituloEmpregado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTituloCpf)
                    .addComponent(lblConteudoCpf))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTituloValor)
                    .addComponent(txtValorSolicitado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTituloNumParcelas)
                    .addComponent(txtNumParcelas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addComponent(separador, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTituloValorPrestacao)
                    .addComponent(lblConteudoValorPrestacao))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTituloValorTotal)
                    .addComponent(lblConteudoValorTotal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(lblObrigatorio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSolicitar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void btnSolicitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSolicitarActionPerformed
		try{
			Emprestimo emprestimo = getDadosEmprestimo();
			emprestimo.setJuros( EmprestimoServices.obterJurosSolicitacaoEmprestimo( emprestimo ) );
			preencherDadosRetornoSolicitacaoEmprestimo( emprestimo );
			mostrarComponentesTela( componentesResultado );
		}
		catch (IllegalArgumentException ex){
			SupportWindow.mostrarMensagemInputIncorreta( ex );
		}
		catch (Exception ex) {
			SupportWindow.mostrarMensagemErro( ex );
		}
	}//GEN-LAST:event_btnSolicitarActionPerformed

	private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
		SupportWindow.fecharDialogo( this );
	}//GEN-LAST:event_btnCancelarActionPerformed

	public static void main(String args[]) {
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
		 * If
		 * Nimbus
		 * (introduced
		 * in
		 * Java
		 * SE
		 * 6)
		 * is
		 * not
		 * available,
		 * stay
		 * with
		 * the
		 * default
		 * look
		 * and
		 * feel.
		 * For
		 * details
		 * see
		 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(DlgSolicitarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(DlgSolicitarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(DlgSolicitarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(DlgSolicitarEmprestimo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run() {
				DlgSolicitarEmprestimo dialog = new DlgSolicitarEmprestimo(new javax.swing.JFrame(), true, solicitante.getCpf());
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {

					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}
	
	private static Empregado solicitante;
	private static List< JComponent > componentesResultado;
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSolicitar;
    private javax.swing.JLabel lblConteudoCpf;
    private javax.swing.JLabel lblConteudoNome;
    private javax.swing.JLabel lblConteudoValorPrestacao;
    private javax.swing.JLabel lblConteudoValorTotal;
    private javax.swing.JLabel lblObrigatorio;
    private javax.swing.JLabel lblTituloCpf;
    private javax.swing.JLabel lblTituloEmpregado;
    private javax.swing.JLabel lblTituloNumParcelas;
    private javax.swing.JLabel lblTituloValor;
    private javax.swing.JLabel lblTituloValorPrestacao;
    private javax.swing.JLabel lblTituloValorTotal;
    private javax.swing.JSeparator separador;
    private javax.swing.JTextField txtNumParcelas;
    private javax.swing.JTextField txtValorSolicitado;
    // End of variables declaration//GEN-END:variables

	//-------------------------------------------------------------------------
	// métodos private
	//-------------------------------------------------------------------------	
	private void initForm(String cpfDoSolicitante)
	{
		componentesResultado = getComponentesDeResultado();
		apagarComponentesTela( componentesResultado );
		
		solicitante = getSolicitante( cpfDoSolicitante );
		preencherDadosDoSolicitante();
	}

	private List< JComponent > getComponentesDeResultado()
	{
		List< JComponent > componentes = new ArrayList<>();
		
		componentes.add( lblTituloValorPrestacao );
		componentes.add( lblTituloValorTotal );
		componentes.add( lblConteudoValorPrestacao );
		componentes.add( lblConteudoValorTotal );
		componentes.add( separador );
		
		return( componentes );
	}
	
	private void apagarComponentesTela( List< JComponent > componentes )
	{
		for( JComponent componente : componentes )
			SupportWindow.setToInvisible( componente );
	}

	private void mostrarComponentesTela( List<JComponent> componentes )
	{
		for( JComponent componente : componentes )
			SupportWindow.setToVisible( componente );
	}
	
	private Empregado getSolicitante( String cpfDoSolicitante )
	{
		Empregado empregado = null;
		try {
			empregado = EmpregadoServices.getEmpregado( DAOFactory.getEmpregadoDAO(), cpfDoSolicitante );
		} catch (FileNotFoundException ex) {
			SupportWindow.mostrarMensagemErro( ex );
		}
		return( empregado );
	}

	private void preencherDadosDoSolicitante()
	{
		lblConteudoCpf.setText( solicitante.getCpf() );
		lblConteudoNome.setText( solicitante.getNome() );
	}

	private Emprestimo getDadosEmprestimo()
	{
		Emprestimo emprestimo = new Emprestimo();
		
		emprestimo.setSolicitante( solicitante );
		emprestimo.setNumParcelas( getNumParcelas() );
		emprestimo.setValor( getValorSolicitado() );
		
		return( emprestimo );
	}

	private int getNumParcelas() {
		return( SupportWindow.getParameter( txtNumParcelas.getText(), -1 ) );
	}

	private Dinheiro getValorSolicitado() {
		return( SupportWindow.getParameter( txtValorSolicitado.getText(), (Dinheiro)null ) );
	}

	private void preencherDadosRetornoSolicitacaoEmprestimo(Emprestimo emprestimo)
	{
		lblConteudoValorPrestacao.setText( emprestimo.getValorCadaPrestacao().toString() );
		lblConteudoValorTotal.setText( emprestimo.getValorTotalComJuros().toString() );
	}
}
